import React, { useEffect, useState } from 'react';
import { Button, Toast } from '@ant-design/react-native';
import dayjs from 'dayjs';
import UserStore from '@/stores/userStore';
import LmoNrcStore from './lmonrcStore';
import LmoCardView from './lmoCard';

// LMO 编制/退回修改提交页面
function LmoSubmitPage({ route, navigation }) {
  const [lmoData, setLmoData] = useState({});
  const userStore = UserStore.useStore();
  const lmonrcStore = LmoNrcStore.useStore();
  useEffect(() => {
    // 机号 航站等已确定数据由navigation传递
    if (route.params) {
      if (route.params.id) {
        navigation.setOptions({ title: 'LMO退回修改' });
        setLmoData(route.params);
        //handleLoad(route.params.id);
      } else {
        setLmoData({
          ...route.params,
          submitter: userStore.user.Name,
          submitDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
          acno: route.params.acno,
          sta: route.params.sta,
          taskStageTid: route.params.taskStageTid,
          flightTid: route.params.flightTid,
        });
      }
    }
  }, [route.params]);
  const handleLoad = async (id) => {
    const res = await lmonrcStore.getLmoData(id);
    if (!res.errorCode) {
      setLmoData(res.data);
    }
  };
  const handleLmoChange = (field, value) => {
    setLmoData({ ...lmoData, [field]: value });
  };
  const handleSubmitAudit = async () => {
    const res = await lmonrcStore.submitLmoAudit(
      userStore.user.LoginName,
      lmoData,
    );
    if (!res.errorCode) {
      navigation.goBack();
    } else {
      Toast.fail(res.errorMsg);
    }
  };
  return (
    <>
      <LmoCardView
        lmo={lmoData}
        onFieldChange={handleLmoChange}
        withAudit={!!route.params.id}
      />
      <Button
        type="primary"
        onPress={handleSubmitAudit}
        loading={lmonrcStore.submitLmoAudit.loading}
      >
        提交审批
      </Button>
    </>
  );
}

LmoSubmitPage.routeName = 'LmoSubmitPage';
LmoSubmitPage.permission = 'RLmoSubmit';
export default LmoSubmitPage;
